<template>
  <div class="container">
    <img class="banner" src="@/assets/user-banner.png" alt="" />
    <div class="wrap">
      <div class="title">赛事交流</div>
      <div class="main">
        <div class="left">
          <div class="field">
            <el-input
              type="textarea"
              rows="4"
              placeholder="分享着一颗的想法... ..."
              v-model="content"
            >
            </el-input>
            <div class="bottom">
              <div class="btns">
                <el-upload
                  :action="action"
                  :on-preview="handlePreview"
                  :on-remove="handleRemove"
                  :on-success="handleSuccess"
                  accept="image/*"
                  :limit="9"
                  :file-list="imgList"
                  :headers="{
                    token: token,
                  }"
                >
                  <p class="pointer">
                    <i class="el-icon-picture-outline"></i> 图片
                  </p>
                </el-upload>
                <!-- <el-upload
                  action="http://suipai.dingfengst.com/api/common/upload"
                  :on-preview="handleVideoPreview"
                  :on-remove="handleVideoRemove"
                  :on-success="handleVideoSuccess"
                  accept="video/*"
                  :limit="9"
                  :file-list="videoList"
                  :headers="{
                    token: token,
                  }"
                >
                  <p class="pointer">
                    <i class="el-icon-video-camera"></i>视频
                  </p>
                </el-upload> -->
                <!-- <p class="pointer">
                  <i class="el-icon-picture-outline"></i> 图片
                </p> -->
                <!--  -->
              </div>
              <div class="btn pointer" @click="submit">发布</div>
            </div>
          </div>
          <div class="tabs">
            <p
              :class="tabSelected === `all` ? `active` : ``"
              @click="tabSelected = `all`"
            >
              热议
              <img src="@/assets/active.png" alt="" />
            </p>
            <p
              :class="tabSelected === `self` ? `active` : ``"
              @click="tabSelected = `self`"
            >
              我发布的
              <img src="@/assets/active.png" alt="" />
            </p>
            <!--            <p>关注的赛事 <img src="@/assets/active.png" alt="" /></p>-->
          </div>
          <div class="list">
            <div class="col" v-for="item in list" :key="item.id">
              <div class="item">
                <div class="upper">
                  <div class="info">
                    <div class="avatar">
                      <img :src="item.avatar" alt="" />
                    </div>
                    <div class="text">
                      <p>{{ item.nickname }}</p>
                      <span>{{ item.publishtime_text }}</span>
                    </div>
                  </div>
                  <div
                    class="btn pointer del"
                    @click="handleDel(item)"
                    v-if="tabSelected == `self`"
                  >
                    删除
                  </div>
                  <div
                    class="btn pointer active"
                    @click="follow(item)"
                    v-if="item.follow === 1"
                  >
                    已关注
                  </div>
                  <div class="btn pointer" @click="follow(item)" v-else>
                    关注
                  </div>
                </div>
                <div class="content">
                  {{ item.content }}
                </div>
                <div class="imgList list1" v-if="item.images.length === 1">
                  <img
                    v-for="img in item.images"
                    :key="img.id"
                    :src="img"
                    alt=""
                  />
                </div>
                <div class="imgList list2" v-else>
                  <p v-for="img in item.images" :key="img.id">
                    <img :src="img" alt="" />
                  </p>
                </div>
              </div>
            </div>
          </div>
          <pagination
            v-if="total > 0"
            :total="total"
            :currentPage="page"
            :pageSize="rows"
            @handleCurrentChange="handleCurrentChange"
          />
        </div>
        <div class="right">
          <div class="rightTitle">可能感兴趣的人</div>
          <div class="list">
            <div
              class="col"
              v-for="item in interested"
              :key="item.id"
              @click="goUserDetail(item)"
            >
              <div class="item">
                <div class="avatar">
                  <img :src="item.avatar" alt="" />
                </div>
                <div class="text">
                  <div class="name">
                    {{ item.nickname }}
                    <!-- <span>刚刚</span> -->
                  </div>
                  <!-- <p class="ovh">秦尼确认《GTA5》下月将从 PlayStation...</p> -->
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <el-dialog :visible.sync="dialogVisible">
      <img v-if="dialogImageUrl" width="100%" :src="dialogImageUrl" alt="" />
      <video
        controls
        autoplay
        v-if="dialogVideoUrl"
        :src="dialogVideoUrl"
      ></video>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "NavBar",
  components: {
    pagination: () => import("@/components/pagination.vue"),
  },
  data() {
    return {
      action: "",
      content: "",
      imgList: [],
      token: "",
      videoList: [],
      dialogVisible: false,
      dialogImageUrl: "",
      dialogVideoUrl: "",
      page: 1,
      rows: 10,
      total: 0,
      list: [],
      interested: [],
      tabSelected: "all",
    };
  },
  mounted() {
    this.action = this.BASE_URL + "/common/upload";
    this.token = localStorage.getItem("token");
    this.getList();
    this.getInterested();
  },
  watch: {
    tabSelected: function (newVal, oldVal) {
      // console.log(newVal,oldVal);
      this.page = 1;
      this.getList();
    },
  },
  methods: {
    getList() {
      this.$post("/index/exchanges_list", {
        page: this.page,
        rows: this.rows,
        tab: this.tabSelected,
      }).then((res) => {
        this.list = res.data.data;
        this.total = res.data.total;
      });
    },
    goUserDetail(item) {
      location.href = "cameramanDetail?id=" + item.id;
    },
    handleDel(item) {
      this.$confirm("您确认删除这条交流？", "删除提醒", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$post("/user/exchange_del", { exchange_id: item.id }).then(
            (res) => {
              if (res.code == 1) {
                this.$message.success("删除成功");
                setTimeout(() => {
                  this.getList();
                }, 1000);
              }
            }
          );
        })
        .catch(() => {
          // 用户点击取消按钮或关闭对话框时执行
        });
    },
    follow(item) {
      let that = this;
      this.$post("/user/follow", { follow_id: item.user_id }).then((res) => {
        if (res.code === 1) {
          if (res.data.result === "Y") {
            item.follow = 1;
            that.$message.success("关注成功");
          }
          if (res.data.result === "N") {
            item.follow = 0;
            that.$message.error("取消关注");
          }
        }
      });
    },
    getInterested() {
      this.$post("/index/exchanges_interested", {
        limit: 6,
      }).then((res) => {
        this.interested = res.data;
      });
    },
    handleCurrentChange(e) {
      this.page = e;
      this.getList();
    },
    handleSuccess(res, file) {
      this.imgList.push({
        name: file.name,
        url: res.data.fullurl,
      });
    },
    handleRemove(file) {
      const index = this.imgList.indexOf(file);
      this.imgList.splice(index, 1);
    },
    handlePreview(file) {
      this.dialogVideoUrl = null;
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleVideoSuccess(res, file) {
      this.videoList.push({
        name: file.name,
        url: res.data.fullurl,
      });
    },
    handleVideoRemove(file) {
      const index = this.videoList.indexOf(file);
      this.videoList.splice(index, 1);
    },
    handleVideoPreview(file) {
      this.dialogImageUrl = null;
      this.dialogVideoUrl = file.url;
      this.dialogVisible = true;
    },
    submit() {
      let list = [];
      this.imgList.forEach((item) => {
        list.push(item.url);
      });
      this.videoList.forEach((item) => {
        list.push(item.url);
      });
      this.$post("/user/exchange_publish", {
        content: this.content,
        images: list.join(","),
      }).then((res) => {
        if (res.code == 1) {
          this.$message.success({
            message: "发布成功",
          });
          this.imgList = [];
          this.videoList = [];
          this.content = "";
          this.getList();
        }
      });
    },
  },
};
</script>

<style scoped lang="scss">
.container {
  font-size: 16px;
}
.banner {
  height: 300px;
}
.title {
  font-size: 58px;
  font-weight: bold;
  text-align: center;
  margin: 30px 0;
}
.main {
  display: flex;
  .left {
    width: calc(100% - 400px);
    padding-right: 40px;
    :deep .el-textarea__inner {
      background: #f0f1f4;
      border: none;
    }
    .field {
      box-shadow: 0 0 10px rgba($color: #000000, $alpha: 0.05);
    }
    .bottom {
      margin-top: 16px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .btns {
        display: flex;
        p {
          font-size: 14px;
          color: #999;
          display: flex;
          flex-direction: column;
          margin-right: 30px;
          i {
            font-size: 24px;
          }
        }
      }
      .btn {
        line-height: 46px;
        color: #fff;
        background: #8c7cb8;
        width: 130px;
        text-align: center;
        border-radius: 100px;
      }
      .btn.active {
        background-color: #999999;
      }
    }
    .tabs {
      display: flex;
      align-items: center;
      padding: 40px 0;
      p {
        font-size: 22px;
        color: #8b8b8b;
        margin-right: 40px;
        position: relative;
        padding-bottom: 14px;
        cursor: pointer;
        img {
          display: none;
          position: absolute;
          left: 50%;
          bottom: 0;
          transform: translateX(-50%);
          width: 30px;
        }
      }
      .active {
        font-size: 26px;
        color: #333;
        img {
          display: block;
        }
      }
    }
    .list {
      .col {
        padding: 10px 0;
        .item {
          padding: 20px;
          box-shadow: 0 0 10px rgba($color: #000000, $alpha: 0.05);
          .upper {
            display: flex;
            align-items: center;
            justify-content: space-between;

            .info {
              display: flex;
              align-items: center;
              .avatar {
                width: 86px;
                height: 86px;
                margin-right: 20px;
                img {
                  border-radius: 50%;
                  height: 100%;
                }
              }
              p {
                font-size: 20px;
              }
              span {
                color: #999;
              }
            }
            .btn {
              line-height: 42px;
              color: #fff;
              background: #8c7cb8;
              width: 110px;
              text-align: center;
              border-radius: 10px;
            }
            .del {
              background-color: #f56c6c;
            }
            .active {
              background-color: #999999;
            }
          }
          .content {
            color: #666;
            padding: 20px 0;
          }
          .list1 {
            img {
              width: 50%;
            }
          }
          .list2 {
            display: flex;
            flex-wrap: wrap;
            p {
              width: 33.33%;
              padding: 5px;
              height: 166px;
              img {
                height: 100%;
              }
            }
          }
        }
      }
    }
  }
  .right {
    width: 400px;
    .rightTitle {
      font-size: 30px;
      font-weight: bold;
      margin-bottom: 30px;
    }
    .list {
      background: #f5f5f5;
      padding: 15px;
      .item {
        display: flex;
        padding: 7px 0;
        .avatar {
          img {
            width: 66px;
            height: 66px;
            border-radius: 50%;
          }
        }
        .text {
          padding-left: 20px;
          .name {
            display: flex;
            align-items: center;
            justify-content: space-between;
            font-size: 20px;
            margin-bottom: 6px;
            span {
              color: #999;
              font-size: 14px;
            }
          }
          p {
            font-size: 14px;
            color: #666;
          }
        }
      }
    }
  }
  .field {
    padding: 20px;
  }
}
@media screen and (max-width: 1024px) {
  .main .right {
    width: 300px;
  }
  .main .left {
    width: calc(100% - 300px);
    padding-right: 20px;
  }
}
@media screen and (max-width: 768px) {
  .title {
    font-size: 24px;
  }
  .main {
    display: block;
    padding-bottom: 30px;
    .left {
      width: 100%;
      padding-right: 0;
    }
    .right {
      width: 100%;
    }
  }
  .main .right .rightTitle {
    font-size: 20px;
    margin-bottom: 20px;
  }
  .main .right .list .item .text .name {
    font-size: 18px;
  }
  .main .left .tabs {
    // justify-content: space-between;
    .active {
      font-size: 20px;
    }
    p {
      font-size: 18px;
      margin-right: 30px;
    }
  }
  .main .left .bottom .btn {
    line-height: 40px;
    width: 88px;
  }
  .main .left .list .col .item .upper .info .avatar {
    width: 50px;
    height: 50px;
    margin-right: 10px;
  }
  .main .left .list .col .item .upper .info {
    p {
      font-size: 16px;
    }
    span {
      font-size: 14px;
    }
  }
  .main .left .list .col .item .upper .btn {
    line-height: 36px;
    width: 70px;
    border-radius: 6px;
    font-size: 14px;
  }
  .main .left .list .col .item .list2 p {
    width: 50%;
    height: 140px;
  }
}
</style>
